package com.lbs.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lbs.entity.User;
import com.lbs.service.Impl.UserServiceImpl;
import com.lbs.service.UserService;
import com.lbs.utils.BaseServlet;
import com.lbs.utils.PageUtils;
import com.lbs.utils.ResultVO;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * @author Administrator
 * @date 2021/5/29 13:46
 * @description
 **/
@WebServlet("/user")
public class UserServlet extends BaseServlet {

    ResultVO resultVO = null;

    ObjectMapper mapper = new ObjectMapper();

    UserService userService = new UserServiceImpl();

    /**
     * 用户登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String imageCode = request.getParameter("imageCode");
        HttpSession session = request.getSession();
        String verCode = (String) session.getAttribute("verCode");
        if (verCode.equalsIgnoreCase(imageCode)){
            User user = userService.login(username, password);
            if (user!=null&&!user.equals("")){
                if (user.getRole() == 1){
                    resultVO = new ResultVO(4,"登录成功！",user);
                    session.setAttribute("user",user);
                }else {
                    resultVO = new ResultVO(3,"抱歉，您没有权限登录！",null);
                }
            }else {
                resultVO = new ResultVO(2,"用户名或密码错误，请重新输入！",null);
            }
        }else {
            resultVO = new ResultVO(1,"验证码输入错误，请重新输入！",null);
        }
        String json = mapper.writeValueAsString(resultVO);
        response.getWriter().print(json);

    }

    /**
     * 回显用户名
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void getUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();

        User user = (User) session.getAttribute("user");

        if (user!=null){
            resultVO = new ResultVO(200,"获取用户信息成功！",user);
        }else {
            resultVO = new ResultVO(500,"获取用户信息失败，请先登录！",null);
        }

        response.getWriter().print(mapper.writeValueAsString(resultVO));

    }

    /**
     * 退出登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        resultVO = new ResultVO(200,"退出登录成功！",null);
        response.getWriter().print(mapper.writeValueAsString(resultVO));
    }

    /**
     * 添加用户信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        User user = new User();
        BeanUtils.populate(user,request.getParameterMap());
        boolean flag = userService.add(user);
        if (flag){
            resultVO = new ResultVO(200,"添加用户成功！",null);
        }else {
            resultVO = new ResultVO(500,"添加用户失败！",null);
        }

        response.getWriter().print(mapper.writeValueAsString(resultVO));

    }

    /**
     * 分页查询用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void pageUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String search = request.getParameter("search");
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        int currentPage = Integer.parseInt(request.getParameter("currentPage"));
        PageUtils<User> pageUntils = userService.findUserByName(search, pageSize, currentPage);

        if (pageUntils!=null){
            resultVO = new ResultVO(200,"查询成功",pageUntils);
        }else {
            resultVO = new ResultVO(500,"查询失败",null);
        }

        response.getWriter().print(mapper.writeValueAsString(resultVO));

    }

    /**
     * 批量删除用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void batchDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] uids = request.getParameterValues("uid");
        boolean flag = userService.batchDel(uids);
        if (flag){
            resultVO = new ResultVO(200,"删除成功",null);
        }else {
            resultVO = new ResultVO(500,"删除失败",null);
        }
        response.getWriter().print(mapper.writeValueAsString(resultVO));
    }

    /**
     * 修改用户信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        User user = new User();
        BeanUtils.populate(user,request.getParameterMap());
        boolean flag = userService.updateUser(user);
        if (flag){
            resultVO = new ResultVO(200,"修改成功",null);
        }else {
            resultVO = new ResultVO(500,"修改失败",null);
        }

        response.getWriter().print(mapper.writeValueAsString(resultVO));

    }
}
